saveBool = false;

subjectList = {...
    'N072912R'...
    'A080512A'...
    'K091412S'...
    'M101012R'...
    'R091412V'...
    };

FFA_t_thresh = 5;
ADAPT_t_thresh = 1.65;


numSubjects = length(subjectList);
summaryResults = cell(numSubjects,1);
%analysesDir = '/Volumes/cluster/jet/mattar/AdaptID/Analyses/';
analysesDir = '/Users/marcelomattar/Desktop/Analysis_bkp/_SingleAdaptCovariate/';
index_of_1_back_adaptation_in_feat = 2;
index_of_FFAroi_in_FOBSdata = 1;


for subjIndx = 1:numSubjects
    
    [summaryResults{subjIndx,1}, averageBetas] = extractROImask(subjectList{subjIndx}, saveBool, FFA_t_thresh, ADAPT_t_thresh, analysesDir, index_of_1_back_adaptation_in_feat, index_of_FFAroi_in_FOBSdata);
    display(averageBetas);
    
end


% Plot Average Betas per scan
AverageBetas_figure = figure(1);
set(AverageBetas_figure, 'Position', [500 200 1200 1200])
for subjIndx = 1:numSubjects

    % Plot Average Beta for FOBS roi
    subplot(numSubjects,3,(subjIndx-1)*3 + 1);
    bar(summaryResults{subjIndx}{2,2});
    axis([0 5 -30 30]);
    ylabel(summaryResults{subjIndx}{1,1});
    if subjIndx==1; title('FOBS roi'); end;

    % Plot Average Beta for ADAPT roi
    subplot(numSubjects,3,(subjIndx-1)*3 + 2);
    bar(summaryResults{subjIndx}{3,2});
    axis([0 5 -30 30]);
    if subjIndx==1; title('ADAPT roi'); end;

    % Plot Average Beta for INTERSECTION roi
    subplot(numSubjects,3,(subjIndx-1)*3 + 3);
    bar(summaryResults{subjIndx}{4,2});
    axis([0 5 -30 30]);
    if subjIndx==1; title('INTERSECTION roi'); end;

end



% Plot Average tstats per scan
AverageTstats_figure = figure(2);
set(AverageTstats_figure, 'Position', [500 200 1200 1200])
for subjIndx = 1:numSubjects

    % Plot Average Beta for FOBS roi
    subplot(numSubjects,3,(subjIndx-1)*3 + 1);
    bar(summaryResults{subjIndx}{2,4});
    axis([0 5 -2 2]);
    ylabel(summaryResults{subjIndx}{1,1});
    if subjIndx==1; title('FOBS roi'); end;

    % Plot Average Beta for ADAPT roi
    subplot(numSubjects,3,(subjIndx-1)*3 + 2);
    bar(summaryResults{subjIndx}{3,4});
    axis([0 5 -2 2]);
    if subjIndx==1; title('ADAPT roi'); end;

    % Plot Average Beta for INTERSECTION roi
    subplot(numSubjects,3,(subjIndx-1)*3 + 3);
    bar(summaryResults{subjIndx}{4,4});
    axis([0 5 -2 2]);
    if subjIndx==1; title('INTERSECTION roi'); end;

end



% Plot coeficient of variation of Betas per scan
AverageCoefVar_figure = figure(3);
set(AverageCoefVar_figure, 'Position', [500 200 1200 1200])
for subjIndx = 1:numSubjects

    % Plot Average Beta for FOBS roi
    subplot(numSubjects,3,(subjIndx-1)*3 + 1);
    bar(abs(summaryResults{subjIndx}{2,6}));
    axis([0 5 0 20]);
    ylabel(summaryResults{subjIndx}{1,1});
    if subjIndx==1; title('FOBS roi'); end;

    % Plot Average Beta for ADAPT roi
    subplot(numSubjects,3,(subjIndx-1)*3 + 2);
    bar(abs(summaryResults{subjIndx}{3,6}));
    axis([0 5 0 20]);
    if subjIndx==1; title('ADAPT roi'); end;

    % Plot Average Beta for INTERSECTION roi
    subplot(numSubjects,3,(subjIndx-1)*3 + 3);
    bar(abs(summaryResults{subjIndx}{4,6}));
    axis([0 5 0 20]);
    if subjIndx==1; title('INTERSECTION roi'); end;

    
end


% Plot summary of Betas
SummaryBetas_figure = figure(4);
set(SummaryBetas_figure, 'Position', [500 200 1000 400]);
FOBS_average_betas = zeros(numSubjects,2);
ADAPT_average_betas = zeros(numSubjects,2);
INTERSECTION_average_betas = zeros(numSubjects,2);
for subjIndx = 1:numSubjects
    
    FOBS_average_betas(subjIndx,1) = mean(summaryResults{subjIndx}{2,2});
    FOBS_average_betas(subjIndx,2) = std(summaryResults{subjIndx}{2,2});
    
    ADAPT_average_betas(subjIndx,1) = mean(summaryResults{subjIndx}{3,2});
    ADAPT_average_betas(subjIndx,2) = std(summaryResults{subjIndx}{3,2});
    
    INTERSECTION_average_betas(subjIndx,1) = mean(summaryResults{subjIndx}{4,2});
    INTERSECTION_average_betas(subjIndx,2) = std(summaryResults{subjIndx}{4,2});
end

subplot(1,3,1);
bar(FOBS_average_betas(:,1))
hold on
errorbar(FOBS_average_betas(:,1),FOBS_average_betas(:,2));
axis([0 6 -20 30]);
title('FOBS roi');
xlabel('Subject');
ylabel('Average Beta');

subplot(1,3,2);
bar(ADAPT_average_betas(:,1))
hold on
errorbar(ADAPT_average_betas(:,1),ADAPT_average_betas(:,2));
axis([0 6 -20 30]);
title('ADAPT roi');
xlabel('Subject');

subplot(1,3,3);
bar(INTERSECTION_average_betas(:,1))
hold on
errorbar(INTERSECTION_average_betas(:,1),INTERSECTION_average_betas(:,2));
axis([0 6 -20 30]);
title('INTERSECTION roi');
xlabel('Subject');



